package com.dhcc.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.dhcc.model.Student;
import com.dhcc.model.Teacher;
import com.dhcc.model.User;
import com.dhcc.model.UserInsert;
import com.dhcc.service.UserService;

/**
 * Created by guorui on 2017/3/8.
 */

@Controller
@RequestMapping("admin")
public class AdminContoller {
	@Autowired
	private UserService userService;
	
    private static final String SUCCESS = "success";

    @RequestMapping(value="/add",method = RequestMethod.POST)
    public String insertUser(User user){
    	return SUCCESS;
    }
    
    @RequestMapping(value="/query",method = RequestMethod.GET)
    public String getUser(User user){
		List<User> users = userService.query();
		System.out.println("-=-------"+users.get(0));
		return SUCCESS;
    	
    }
    @RequestMapping("insertUser")
    public String insertUser(){
    	return "admin/insert_user";
    }
    /**
     * 根据ID查询用户信息
     * @param request
     * @return
     */
    int id=1;
    @RequestMapping("priuserManager")
    public ModelAndView priuserManager(HttpServletRequest request) {
    	 Student stu=userService.findStudentByUserId(id);
    	 int userId=stu.getUserId();
    	 User user=userService.findUserByUserId(userId);
		 request.getSession().setAttribute("stu", stu);
		 request.getSession().setAttribute("user", user);
		 return new ModelAndView("admin/priuser_manager");
	}
    /**
     * 根据ID查询要修改的信息
     * @param request
     * @return
     */
    @RequestMapping("updateUser")
    public ModelAndView updateUser(HttpServletRequest request) {
    	Student stu=userService.findStudentByUserId(id);
    	int userId=stu.getUserId();
   	    User user=userService.findUserByUserId(userId);
		request.getSession().setAttribute("stu", stu);
		request.getSession().setAttribute("user", user);
		return new ModelAndView("admin/update_user");
    }
    /**
     * 根据ID修改用户信息
     * @param s
     * @param u
     * @param request
     * @return
     */
    @RequestMapping("upUser")
    public ModelAndView upUser(Student s,User u,HttpServletRequest request) {
    	 userService.upStudent(s);
    	 userService.upUser(u);
    	 Student stu=userService.findStudentByUserId(id);
    	 int userId=stu.getUserId();
    	 User user=userService.findUserByUserId(userId);
		 request.getSession().setAttribute("stu", stu);
		 request.getSession().setAttribute("user", user);
		 return new ModelAndView("admin/priuser_manager");
		
	}
    
    /**
     * 分页显示用户信息
     * @param request
     * @return
     */
    
    public static int page;
    @RequestMapping("userManager")
    public ModelAndView userManager(HttpServletRequest request){
    	page=1;
    	List<Student> student=userService.selectStudent(page);
    	int stuSize=student.size();
    	List<Teacher> teacher=userService.selectTeacher(page);
    	int teaSize=teacher.size();
    	int size=stuSize+teaSize;
    	ModelAndView mv=new ModelAndView("admin/user_manager");
    	mv.addObject("teas",teacher);
    	mv.addObject("stus", student);
    	mv.addObject("page", page);
    	mv.addObject("size", size);
    	return mv;
    }
    
    @RequestMapping("newUser")
    public ModelAndView newUser(UserInsert insert){
    	System.out.println(insert);
    	int userId=insert.getUserId();
    	if(userId==1){
    		userService.insertTeacher(insert);
    	}else if(userId==2){
    		userService.insertStudent(insert);
    	}
    	List<Student> student=userService.selectStudent(page);
    	int stuSize=student.size();
    	List<Teacher> teacher=userService.selectTeacher(page);
    	int teaSize=teacher.size();
    	int size=stuSize+teaSize;
    	ModelAndView mv=new ModelAndView("admin/user_manager");
    	mv.addObject("teas",teacher);
    	mv.addObject("stus", student);
    	mv.addObject("page", page);
    	mv.addObject("size", size);
    	return mv;
    }
}
